package com.ymatou.iapi.trading.operate.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.RollbackToWaitDeliveryBean;
import com.ymatou.iapi.trading.operate.parameter.req.FrozenOrUnFrozenOrderBean;
import com.ymatou.iapi.trading.operate.service.FrozenOrUnFrozenOrderCall;
import com.ymatou.iapi.trading.operate.service.RollbackToWaitDeliveryCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.TestDataManager;
/**
*订单回滚到待发货接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_RollbackToWaitDelivery {
	private static RollbackToWaitDeliveryBean rollbacktowaitdeliveryBean;
	private static RollbackToWaitDeliveryCall rollbacktowaitdeliveryCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("订单回滚到待发货接口");
	}

	@Before
	public void caseUp() {
		rollbacktowaitdeliveryBean = new RollbackToWaitDeliveryBean();
		rollbacktowaitdeliveryCall = new RollbackToWaitDeliveryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(rollbacktowaitdeliveryCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/*
	 * 修改数据，删除运单编号
	 * */
	private static void onXloboBillDeletedCall(String billNo)
			throws ClientProtocolException, IOException {

		new DefaultWapper()
				.executeUpdateSql(String
						.format("UPDATE Ymt_BillCodeSummary SET IsDeleted=1,UpdateTime=GETDATE() where billCode = '%s' and IsDeleted=0",
								billNo));

	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RollbackToWaitDelivery_001() {
		Logger.start(true, "订单回滚到待发货成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			OrderCallService orderCallService = new OrderCallService();
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.placeOrderWithOutCoupon();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();
			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);

			String billNo = String.format("YD%s",
					TestDataManager.getRandomNum(10));

			// 码头保存物流单号
			PlaceOrderService.saveDeliverSummary(orderId, sellerId, billNo);

			// 商家发货
			orderCallService.dispatchedByXlobo(orderId, sellerId);

			//删除运单
			onXloboBillDeletedCall(billNo);

			Logger.comment("前置数据准备结束");

			String operateUser = "sys";

			String reason = "autotest rollbac";

			rollbacktowaitdeliveryBean.setOrderId(orderId);
			rollbacktowaitdeliveryBean.setOperateUser(operateUser);
			rollbacktowaitdeliveryBean.setReason(reason);
			rollbacktowaitdeliveryCall.setData(rollbacktowaitdeliveryBean);
			rollbacktowaitdeliveryCall.callService();

			Logger.verifyEquals("200",
					rollbacktowaitdeliveryCall.getString("code"), "验证code");
			Logger.verifyEquals("0",
					rollbacktowaitdeliveryCall.getString("bcode"), "验证bcode");
			Logger.verifyEquals(true, rollbacktowaitdeliveryCall.getData()
					.getBoolean("success"), "验证success");

			DefaultWapper database = new DefaultWapper();
			// 订单主表
			List<Map> ymtOrders = database.selectOrderByOderId(orderId);
			List<Map> list = new DefaultWapper()
					.selectOrderFrozenDetailByOrderId(orderId);
			Logger.verifyEquals(0, list.size(),
					"验证Ymt_OrderFrozenDetail 表数据已删除");
			Logger.verifyEquals(17,
					(Integer) ymtOrders.get(0).get("iTradingStatus"),
					"验证iTradingStatus");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_RollbackToWaitDelivery_002() {
		Logger.start(true, "订单号不存在");
		try {
			Integer orderId = 1111111;

			String operateUser = "sys";
			String reason = "autotest rollbac";

			rollbacktowaitdeliveryBean.setOrderId(orderId);
			rollbacktowaitdeliveryBean.setOperateUser(operateUser);
			rollbacktowaitdeliveryBean.setReason(reason);
			rollbacktowaitdeliveryCall.setData(rollbacktowaitdeliveryBean);
			rollbacktowaitdeliveryCall.callService();

			Logger.verifyEquals("201",
					rollbacktowaitdeliveryCall.getString("code"), "验证code");
			Logger.verifyEquals("101",
					rollbacktowaitdeliveryCall.getString("bcode"), "验证bcode");
			Logger.verifyEquals(false, rollbacktowaitdeliveryCall.getData()
					.getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_RollbackToWaitDelivery_003() {
		Logger.start(true, "订单号为null");
		try {

			String operateUser = "sys";
			String reason = "autotest rollbac";

			//rollbacktowaitdeliveryBean.setOrderId(orderId);
			rollbacktowaitdeliveryBean.setOperateUser(operateUser);
			rollbacktowaitdeliveryBean.setReason(reason);
			rollbacktowaitdeliveryCall.setData(rollbacktowaitdeliveryBean);
			rollbacktowaitdeliveryCall.callService();

			Logger.verifyEquals("400",
					rollbacktowaitdeliveryCall.getString("code"), "验证code");
			Logger.verifyEquals("100",
					rollbacktowaitdeliveryCall.getString("bcode"), "验证bcode");
			Logger.verifyEquals("orderId:订单号未提供",
					rollbacktowaitdeliveryCall.getString("msg"), "验证msg");
			Logger.verifyEquals(false, rollbacktowaitdeliveryCall.getData()
					.getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_RollbackToWaitDelivery_004() {
		Logger.start(true, "操作人为null");
		try {
			Integer orderId = 1111111;

			//String operateUser="autotest";
			String reason = "autotest rollbac";

			rollbacktowaitdeliveryBean.setOrderId(orderId);
			//rollbacktowaitdeliveryBean.setOperateUser(operateUser);
			rollbacktowaitdeliveryBean.setReason(reason);
			rollbacktowaitdeliveryCall.setData(rollbacktowaitdeliveryBean);
			rollbacktowaitdeliveryCall.callService();

			Logger.verifyEquals("400",
					rollbacktowaitdeliveryCall.getString("code"), "验证code");
			Logger.verifyEquals("100",
					rollbacktowaitdeliveryCall.getString("bcode"), "验证bcode");
			Logger.verifyEquals("operateUser:操作人未提供",
					rollbacktowaitdeliveryCall.getString("msg"), "验证msg");
			Logger.verifyEquals(false, rollbacktowaitdeliveryCall.getData()
					.getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_RollbackToWaitDelivery_005() {
		Logger.start(true, "订单状态不是已发货状态");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrderResp = PlaceOrderService
					.placeOrderWithOutCoupon();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");

			String operateUser = "sys";
			String reason = "autotest rollbac";

			rollbacktowaitdeliveryBean.setOrderId(orderId);
			rollbacktowaitdeliveryBean.setOperateUser(operateUser);
			rollbacktowaitdeliveryBean.setReason(reason);
			rollbacktowaitdeliveryCall.setData(rollbacktowaitdeliveryBean);
			rollbacktowaitdeliveryCall.callService();

			Logger.verifyEquals("201",
					rollbacktowaitdeliveryCall.getString("code"), "验证code");
			Logger.verifyEquals("102",
					rollbacktowaitdeliveryCall.getString("bcode"), "验证bcode");
			Logger.verifyEquals("当前订单" + orderId + "状态不匹配",
					rollbacktowaitdeliveryCall.getString("msg"), "验证msg");
			Logger.verifyEquals(false, rollbacktowaitdeliveryCall.getData()
					.getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_RollbackToWaitDelivery_006() {
		Logger.start(true, "订单不是用贝海发货");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			OrderCallService orderCallService = new OrderCallService();
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.placeOrderWithOutCoupon();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();
			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);

			orderCallService.dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");

			String operateUser = "sys";
			String reason = "autotest rollbac";

			rollbacktowaitdeliveryBean.setOrderId(orderId);
			rollbacktowaitdeliveryBean.setOperateUser(operateUser);
			rollbacktowaitdeliveryBean.setReason(reason);
			rollbacktowaitdeliveryCall.setData(rollbacktowaitdeliveryBean);
			rollbacktowaitdeliveryCall.callService();

			Logger.verifyEquals("201",
					rollbacktowaitdeliveryCall.getString("code"), "验证code");
			Logger.verifyEquals("115",
					rollbacktowaitdeliveryCall.getString("bcode"), "验证bcode");
			Logger.verifyEquals("当前订单" + orderId + "非贝海发货",
					rollbacktowaitdeliveryCall.getString("msg"), "验证msg");
			Logger.verifyEquals(false, rollbacktowaitdeliveryCall.getData()
					.getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_RollbackToWaitDelivery_007() {
		Logger.start(true, "改订单码头没有运单号记录");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			OrderCallService orderCallService = new OrderCallService();
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.placeOrderWithOutCoupon();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();
			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 商家发货
			orderCallService.dispatchedByXlobo(orderId, sellerId);
			Logger.comment("前置数据准备结束");

			String operateUser = "sys";
			String reason = "autotest rollbac";

			rollbacktowaitdeliveryBean.setOrderId(orderId);
			rollbacktowaitdeliveryBean.setOperateUser(operateUser);
			rollbacktowaitdeliveryBean.setReason(reason);
			rollbacktowaitdeliveryCall.setData(rollbacktowaitdeliveryBean);
			rollbacktowaitdeliveryCall.callService();

			Logger.verifyEquals("201",
					rollbacktowaitdeliveryCall.getString("code"), "验证code");
			Logger.verifyEquals("116",
					rollbacktowaitdeliveryCall.getString("bcode"), "验证bcode");
			Logger.verifyEquals("当前订单" + orderId + "没有运单号",
					rollbacktowaitdeliveryCall.getString("msg"), "验证msg");
			Logger.verifyEquals(false, rollbacktowaitdeliveryCall.getData()
					.getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_RollbackToWaitDelivery_008() {
		Logger.start(true, "订单有未删除面单");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			OrderCallService orderCallService = new OrderCallService();
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.placeOrderWithOutCoupon();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();
			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);

			String billNo = String.format("YD%s",
					TestDataManager.getRandomNum(10));

			// 码头保存物流单号
			PlaceOrderService.saveDeliverSummary(orderId, sellerId, billNo);

			// 商家发货
			orderCallService.dispatchedByXlobo(orderId, sellerId);
			Logger.comment("前置数据准备结束");

			String operateUser = "autouser";
			String reason = "autotest rollbac";

			rollbacktowaitdeliveryBean.setOrderId(orderId);
			rollbacktowaitdeliveryBean.setOperateUser(operateUser);
			rollbacktowaitdeliveryBean.setReason(reason);
			rollbacktowaitdeliveryCall.setData(rollbacktowaitdeliveryBean);
			rollbacktowaitdeliveryCall.callService();

			Logger.verifyEquals("201",
					rollbacktowaitdeliveryCall.getString("code"), "验证code");
			Logger.verifyEquals("116",
					rollbacktowaitdeliveryCall.getString("bcode"), "验证bcode");
			Logger.verifyEquals(String.format("订单%s对应的所有面单未全部删除",
					rollbacktowaitdeliveryBean.getOrderId()),
					rollbacktowaitdeliveryCall.getString("msg"), "验证msg");
			Logger.verifyEquals(false, rollbacktowaitdeliveryCall.getData()
					.getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RollbackToWaitDelivery_009() {
		Logger.start(true, "订单有多个物流单号,其中有贝海未删除运单");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			OrderCallService orderCallService = new OrderCallService();
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.placeOrderWithOutCoupon();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();
			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);

			String billNo1 = String.format("YD%s",
					TestDataManager.getRandomNum(10));

			String billNo2 = String.format("YD%s",
					TestDataManager.getRandomNum(10));
			// 码头保存物流单号
			PlaceOrderService.saveDeliverSummary(orderId, sellerId, billNo1);
			PlaceOrderService.saveDeliverSummary(orderId, sellerId, billNo2);
			// 商家发货
			orderCallService.dispatchedByXlobo(orderId, sellerId);
			Logger.comment("前置数据准备结束");

			// 删除面单 billNo1
			onXloboBillDeletedCall(billNo1);

			String operateUser = "auto";
			String reason = "autotest rollbac";

			rollbacktowaitdeliveryBean.setOrderId(orderId);
			rollbacktowaitdeliveryBean.setOperateUser(operateUser);
			rollbacktowaitdeliveryBean.setReason(reason);
			rollbacktowaitdeliveryCall.setData(rollbacktowaitdeliveryBean);
			rollbacktowaitdeliveryCall.callService();

			Logger.verifyEquals("201",
					rollbacktowaitdeliveryCall.getString("code"), "验证code");
			Logger.verifyEquals("116",
					rollbacktowaitdeliveryCall.getString("bcode"), "验证bcode");
			Logger.verifyEquals(String.format("订单%s对应的所有面单未全部删除",
					rollbacktowaitdeliveryBean.getOrderId()),
					rollbacktowaitdeliveryCall.getString("msg"), "验证msg");
			Logger.verifyEquals(false, rollbacktowaitdeliveryCall.getData()
					.getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RollbackToWaitDelivery_010() {
		Logger.start(true, "订单回滚到待发货,再重新发货成功");
		try {

			// 创建订单
			Logger.comment("前置数据准备");
			OrderCallService orderCallService = new OrderCallService();
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.placeOrderWithOutCoupon();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();
			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额	
			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);

			String billNo = String.format("YD%s",
					TestDataManager.getRandomNum(10));

			// 码头保存物流单号
			PlaceOrderService.saveDeliverSummary(orderId, sellerId, billNo);

			// 商家发货
			orderCallService.dispatchedByXlobo(orderId, sellerId);

			Logger.comment("前置数据准备结束");

			onXloboBillDeletedCall(billNo);

			String operateUser = "sys";
			String reason = "autotest rollbac";

			rollbacktowaitdeliveryBean.setOrderId(orderId);
			rollbacktowaitdeliveryBean.setOperateUser(operateUser);
			rollbacktowaitdeliveryBean.setReason(reason);
			rollbacktowaitdeliveryCall.setData(rollbacktowaitdeliveryBean);
			rollbacktowaitdeliveryCall.callService();

			Logger.verifyEquals("200",
					rollbacktowaitdeliveryCall.getString("code"), "验证code");
			Logger.verifyEquals("0",
					rollbacktowaitdeliveryCall.getString("bcode"), "验证bcode");
			Logger.verifyEquals(true, rollbacktowaitdeliveryCall.getData()
					.getBoolean("success"), "验证success");

			// 商家发货
			orderCallService.dispatchedByXlobo(orderId, sellerId);
			DefaultWapper database = new DefaultWapper();
			// 订单主表
			List<Map> ymtOrders = database.selectOrderByOderId(orderId);

			Logger.verifyEquals(3,
					(Integer) ymtOrders.get(0).get("iTradingStatus"),
					"验证iTradingStatus");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RollbackToWaitDelivery_011() {
		Logger.start(true, "订单有投诉维权,订单回滚到待发货成功");
		try {

			// 创建订单
			Logger.comment("前置数据准备");
			OrderCallService orderCallService = new OrderCallService();
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.placeOrderWithOutCoupon();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();
			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);

			String billNo = String.format("YD%s",
					TestDataManager.getRandomNum(10));

			// 码头保存物流单号
			PlaceOrderService.saveDeliverSummary(orderId, sellerId, billNo);
			// 商家发货
			orderCallService.dispatchedByXlobo(orderId, sellerId);
			//删除面单
			onXloboBillDeletedCall(billNo);

			FrozenOrUnFrozenOrderBean frozenorunfrozenorderBean = new FrozenOrUnFrozenOrderBean();
			FrozenOrUnFrozenOrderCall frozenorunfrozenorderCall = new FrozenOrUnFrozenOrderCall();
			frozenorunfrozenorderBean.setOrderId(orderId);
			frozenorunfrozenorderBean.setUserId(sellerId);
			frozenorunfrozenorderBean.setIsFrozen(true);
			frozenorunfrozenorderCall.setData(false, frozenorunfrozenorderBean);
			frozenorunfrozenorderCall.callService();

			Logger.comment("前置数据准备结束");

			String operateUser = "sys";
			String reason = "autotest rollbac";

			rollbacktowaitdeliveryBean.setOrderId(orderId);
			rollbacktowaitdeliveryBean.setOperateUser(operateUser);
			rollbacktowaitdeliveryBean.setReason(reason);
			rollbacktowaitdeliveryCall.setData(rollbacktowaitdeliveryBean);
			rollbacktowaitdeliveryCall.callService();

			Logger.verifyEquals("200",
					rollbacktowaitdeliveryCall.getString("code"), "验证code");
			Logger.verifyEquals("0",
					rollbacktowaitdeliveryCall.getString("bcode"), "验证bcode");
			Logger.verifyEquals(true, rollbacktowaitdeliveryCall.getData()
					.getBoolean("success"), "验证success");
			DefaultWapper database = new DefaultWapper();
			// 订单主表
			List<Map> ymtOrders = database.selectOrderByOderId(orderId);

			List<Map> list = new DefaultWapper()
					.selectOrderFrozenByOrderId(orderId);
			Logger.verifyEquals(true, (Boolean) list.get(0).get("bFrozen"),
					"验证Ymt_Order_Frozen 表 bFrozen");
			Logger.verifyIsContains("发起投诉->删除运单",
					(String) list.get(0).get("sMemo"),
					"验证Ymt_Order_Frozen 表 sMemo");

			Logger.verifyEquals(17,
					(Integer) ymtOrders.get(0).get("iTradingStatus"),
					"验证iTradingStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}